/**
 * 
 */
package com.nextive.interview.activity.view;

import java.util.HashMap;
import java.util.Map;

import com.nextive.interview.ApplicationManager;

/**
 * Utility class used as a holder of messages stored within the application's context.
 * @author Cesar Roman
 *
 */
public class DefaultTextValuesHolder {
	
	/**
	 * Map containing messages declared within the application's context.
	 */
	private static Map<Integer, CharSequence> valuesHolderMap = new HashMap<Integer, CharSequence>();
	
	/**
	 * It fetches the message for the given source key.
	 * @param source
	 * @return
	 */
	public static CharSequence fetchValueFor(int source) {
		
		if (valuesHolderMap.containsKey(source))
			return valuesHolderMap.get(source);
		else
			return insertValueFor(source);
		
	}
	
	/**
	 * It checks if given message exists within the context and inserts 
	 * the new value into the map in case it exists.
	 * @param source key to look up the message at
	 * @return desired message. null if it doesn't exist
	 */
	private static CharSequence insertValueFor(int source) {
		
		CharSequence message = ApplicationManager.getAppContext().getText(source);
		if (message == null)
			return null;
		
		valuesHolderMap.put(source, message);
		return message;
		
	}

}